package eg.edu.eelu.fyp2013.jdetector.core.filters.impl;

import java.awt.Image;
import java.awt.image.BufferedImage;

import eg.edu.eelu.fyp2013.jdetector.core.filters.WindowFilter;

public abstract class AbstractWindowFilter implements WindowFilter {

	protected int filterWidth;
	protected int filterHeight;




	protected AbstractWindowFilter(int filterWidth, int filterHeight) {
		this.filterWidth = filterWidth;
		this.filterHeight = filterHeight;
	}




	@Override
	public Image applyFilter(BufferedImage inImage) {
		
		//NO transparency currently
		BufferedImage outImage = new BufferedImage(inImage.getWidth(), inImage.getHeight(), BufferedImage.TYPE_INT_RGB);
		//int[] window= new int[filterWidth * filterHeight];
		int[][] window= new int[filterWidth][filterHeight];
		
		int[][] redPlane= new int[filterWidth][filterHeight],
				greenPlane= new int[filterWidth][filterHeight],
				bluePlane= new int[filterWidth][filterHeight];
		int shiftWidth=filterWidth/2;
		int shiftHeight=filterHeight/2;
		BufferedImage sourceImage = new BufferedImage (inImage.getWidth()+2*shiftWidth, inImage.getHeight()+2*shiftHeight, BufferedImage.TYPE_INT_RGB );
		//sourceImage.setRGB (shiftWidth , shiftHeight, inImage.getWidth(), inImage.getHeight(), inImage.getRGB(0, 0,inImage.getWidth(), inImage.getHeight(),null,0,1) , 0, 1);
		
		for (int i = 0; i < inImage.getWidth(); i++) {
			for (int j = 0; j < shiftHeight; j++) {
				sourceImage.setRGB(i+shiftWidth, j, inImage.getRGB(i, 0));
				sourceImage.setRGB(i+shiftWidth, sourceImage.getHeight()-j-1, inImage.getRGB(i, inImage.getHeight()-1));
			}
			
			
		}//*/
		
		for (int i = 0; i < inImage.getHeight(); i++) {
			for (int j = 0; j < shiftWidth; j++) {
				sourceImage.setRGB(j, i+shiftHeight, inImage.getRGB(0, i));
				sourceImage.setRGB(sourceImage.getWidth()-j-1, i+shiftHeight, inImage.getRGB(inImage.getWidth()-1, i));
			}
			
		}//*/
		
		for (int i = 0; i < shiftWidth; i++) {
			for (int j = 0; j < shiftHeight; j++) {
				sourceImage.setRGB(i, j, inImage.getRGB(0, 0));
				sourceImage.setRGB(sourceImage.getWidth()-i-1, j, inImage.getRGB(inImage.getWidth()-1, 0));
				sourceImage.setRGB(i, sourceImage.getHeight()-j-1, inImage.getRGB(0, inImage.getHeight()-1));
				sourceImage.setRGB(sourceImage.getWidth()-i-1, sourceImage.getHeight()-j-1, inImage.getRGB(inImage.getWidth()-1, inImage.getHeight()-1));
			}

		}//*/
		
		//sourceImage = inImage.getSubimage(0, 0, inImage.getWidth(), inImage.getHeight());
		for (int i = 0; i < inImage.getHeight(); i++) {
			for (int j = 0; j < inImage.getWidth(); j++) {
				
				sourceImage.setRGB(j+shiftWidth, i+shiftHeight, inImage.getRGB(j, i));
			}
			
		}//*/
		for (int i = 0; i < outImage.getHeight(); i++) {
			for (int j = 0; j < outImage.getWidth()  ; j++) {
				//fill the window
				//window = inImage.getRGB(j, i, filterWidth, filterHeight, window, 0, filterWidth);
				for (int k = 0; k < window.length; k++) {
					for (int l = 0; l < window[k].length; l++) {
						window    [k][l]=sourceImage.getRGB(j+l, i+k);
						
						redPlane  [k][l]=0xFF & window[k][l]>>16; 
						greenPlane[k][l]=0xFF & window[k][l]>>8; 
						bluePlane [k][l]=0xFF & window[k][l];
					}
				}
				outImage.setRGB(j , i, (getFilteredPixel(redPlane)<<16)|getFilteredPixel(greenPlane)<<8|getFilteredPixel(bluePlane));
			}
		}
		return outImage;
	}
}
	

